export default {
    data() {
        return {

        }
    },
    computed: {
        basin() {
            return this.$store.getters.basin
        }
    },
    watch: {
        basin: {
            handler: function(name) {
                this.$nextTick(() => {
                    this.creatWATA(name)
                })
            },
            immediate: true
        }
    },
    created() {
        this._entitiesWATA = []
    },
    methods: {
        creatWATA(name) {
            this.clearWATA()
            if (this.layerList2[1].children.WATA.show) {
                var url = `${mapConfig.resUrlPre}/geojson/${this.basin}/WATA.json`
                this.$axios.get(url).then(res => {
                    var features = res.data.features

                    features.forEach(feature => {
                        var lonlats = []
                        var coordinates = feature.geometry.coordinates[0]
                        if (feature.geometry.type === 'MultiPolygon') {
                            // console.log(feature.geometry.coordinates[0].length)
                            coordinates = feature.geometry.coordinates[0][0]
                            // return
                        }

                        coordinates.forEach(coordinate => {
                            lonlats.push(...coordinate)
                        })
                        var polylineEntity = {
                            polyline: {
                                positions: Cesium.Cartesian3.fromDegreesArray(lonlats),
                                width: 1,
                                // material: new Cesium.PolylineOutlineMaterialProperty({
                                //     color: Cesium.Color.fromCssColorString('rgb(255,219,19)')
                                //     // outlineColor: Cesium.Color.WHITE,
                                //     // outlineWidth: 1
                                // }),
                                material: Cesium.Color.fromCssColorString('rgb(255, 224, 17)'),
                                depthFailMaterial: Cesium.Color.fromCssColorString('rgb(255, 224, 17)'),
                                clampToGround: true,
                                classificationType: Cesium.ClassificationType.TERRAIN // Cesium.ClassificationType.BOTH
                            }
                        }
                        this._entitiesWATA.push(this.$viewer.entities.add(polylineEntity))
                    })
                })
            } else {
                this.clearWATA()
            }
        },
        clearWATA() {
            if (this._entitiesWATA && this._entitiesWATA.length > 0) {
                this._entitiesWATA.forEach(item => {
                    this.$viewer.entities.remove(item)
                })
                this._entitiesWATA = []
            }
        }
    }
}
